<template>
	<view style="margin: auto 90rpx; margin-top: 50rpx;">
		<view class="row col0 font500 size30 align padbottom20 minbottom20">
			头像
			<view class="minau minright0">
				<u-avatar :src="avatar" size="40" @click="onImage"></u-avatar>
			</view>
		</view>
		<view class="row padbottom20 minbottom20 align">
			<view class="col0 font500 size30" style="width: 120rpx;">
				昵称
			</view>
			<u--input placeholder="请输入内容" border="bottom" v-model="name" inputAlign="right"></u--input>
		</view>
		<view class="row padbottom20 minbottom20 align">
			<view class="col0 font500 size30" style="width: 120rpx;">
				手机号
			</view>
			<u--input placeholder="请输入内容" type="number" border="bottom" v-model="phone" inputAlign="right"></u--input>
		</view>
		<view class="mintop40 padtop40">
			<u-button shape="circle" color="linear-gradient( 136deg, #FFBC43 0%, #FFDA97 100%)"
				customStyle="color: #885400;font-weight: 600;" text="保存修改" @click="getAddUser"></u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				avatar: '',
				name: '',
				phone: ''
			}
		},
		onLoad() {

		},
		onShow() {
			//当前页面展示的时候他就执行该生命周期
			this.user_id = uni.getStorageSync('user_id');
			if (this.user_id) {
				//如果存在token
				this.getUserInfo()
				//按正常逻辑来讲 登录成功 我们是不是需要向服务器获取用户的信息？
				//那么我们一起来获取用户信息
			} else {
				this.$com.toast('您还未登录，正在前往登录')
				setTimeout(() => {
					this.$jump.nav('/mypackage/sign')
				}, 1000)
			}
		},
		methods: {
			async getUserInfo() {
				let {
					code,
					msg,
					data
				} = await this.$com.fetchGet("AppletUser/GetUserInfo", {
					id: this.user_id,
				})
				if (code == 1) {
					this.avatar = data.avatar
					this.phone = data.mobile
					this.name = data.nickname
				} else {
					console.log(msg);
				}
			},
			async getAddUser() {
				let {
					code,
					msg,
					data
				} = await this.$com.fetchPut("AppletUser/UserUp", {
					id: this.user_id,
					avatar: this.avatar,
					nickname: this.name,
					mobile: this.phone,
				})
				if (code == 1) {
					this.$com.toast(msg)

					setTimeout(() => {
						this.$jump.switch('/pages/my/my')
					}, 1000)
				} else {
					this.$com.toast(msg)
				}
			},
			onImage() {
				var _this = this
				
				uni.chooseImage({
					// count: 1, // 图片数量
					// sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					// sourceType: ['album', 'camera'], //从相册选择或者拍照
					success: (res) => {
						console.log(123)
						const tempFilePaths = res.tempFilePaths;
						console.log(res)
						uni.uploadFile({
							url: 'https://tlw.ynlckjht.com/api/AppletUpload/specsupload', //上传图片api
							filePath: tempFilePaths[0],
							name: 'file',
							formData: {
								'user': 'test',
								'server': 1
							},
							header: {
								// token: uni.getStorageSync('token')
								// 'content-type': 'multipart/form-data;boundary=nitamayoubing',
							},
							success: (res) => {
								let group = JSON.parse(res.data)
								if (group.code == 1) {
									uni.showToast({
										title: "上传成功",
										icon: "success"
									})
									_this.avatar = group.data
								}

							},
							fail(res) {
								console.log(res);
							}
						});
					},complete(e) {
						console.log(e)
					}
				});

			}
		}
	}
</script>

<style scoped lang="less">
</style>